﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.orderDataSetTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;

namespace dhSystemBLL.orderData
{
    public  class productUnitBLL
    {
        private productUnitTableAdapter _productUnitAdapter = null;
        private productUnitTableAdapter productUnitAdapter
        {
            get
            {
                if (_productUnitAdapter == null) _productUnitAdapter = new productUnitTableAdapter();
                return _productUnitAdapter;
            }
        }
        /// <summary>
        /// 查询产品单位表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public orderDataSet.productUnitDataTable getproductUnitAll()
        {
            try
            {
                return productUnitAdapter.GetDataproductUnitAll();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据单位查询产品单位表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public orderDataSet.productUnitDataTable getproductUnitAllBypname(string unit)
        {
            try
            {
                return productUnitAdapter.GetDataproductUnitALLByunit(unit);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
      
        /// <summary>
        /// 插入产品单位表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int productUnitInsert(productUnitModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                productUnitDAL productUnit = new productUnitDAL(tn, conn);
                productUnit.productUnitInsert(row);
                tn.Commit();
                return i;

            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个产品单位或者名称已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
                
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 更新产品单位表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int productUnitUpdate(productUnitModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                productUnitDAL productUnit = new productUnitDAL(tn, conn);
                productUnit.productUnitUpdate (row);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
                
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 删除一行产品单位表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int productUnitDel(string pname)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                productUnitDAL productUnit = new productUnitDAL(tn, conn);
                productUnit.productUnitDel (pname );
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
                
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
